import * as monaco from 'monaco-editor';
import {ICookie, IStringMap} from "@/interface/common";
import {oneDayMillims} from "@/common/common";

export function log(message: any) {
  console.log('%c%s', 'color: green;', '[app-front] ' + message);
}

export const getCookie = (key: string): string => {
  const map: IStringMap = {};
  document.cookie.split(';').map((kv) => {
    const d = kv.trim().split('=');
    map[d[0]] = d[1];
    return null;
  });
  return map[key];
};

export const setCookie = (cData: ICookie[]) => {
  const date = new Date();
  cData.forEach(ele => {
    date.setTime(date.getTime() + (ele.time * oneDayMillims));
    const expires = 'expires=' + date.toUTCString();
    document.cookie = ele.key + '=' + ele.value + '; ' + expires + '; path=/';
  });
};

export const deleteCookie = (cData: string[]) => {
  setCookie(cData.map(i => ({key: i, value: '', time: -1})));
};

/**
 * 返回顶部功能  主要用于翻页时
 */
export function BackToTop() {
  const scrollDom = document.getElementsByClassName("ant-layout-content")[0];
  if (!scrollDom) return;
  let distance = scrollDom.scrollTop;

  if (distance === 0) return;
  const timer = setInterval(() => {
    distance -= 500;
    document.getElementsByClassName("ant-layout-content")[0].scrollTop = distance;
    if (distance <= 0) {
      clearInterval(timer);
    }
  }, 50);
}

export const editorShortCut = (editor: monaco.editor.IStandaloneCodeEditor) => {
  // 运行
  editor.addAction({
    id: 'job_run',
    label: 'Run',
    keybindings: [
      // tslint:disable-next-line:no-bitwise
      monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_E,
    ],
    contextMenuGroupId: 'navigation',
    contextMenuOrder: 2,
    run: () => {
      // TODO:
    },
  });

  // 停止
  editor.addAction({
    id: 'job_stop',
    label: 'Stop',
    keybindings: [
      // tslint:disable-next-line:no-bitwise
      monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_K,
    ],
    contextMenuGroupId: 'navigation',
    contextMenuOrder: 2.5,
    run: () => {
      // TODO:
    },
  });

  editor.onDidChangeModelContent(event => {
    const value = editor.getValue();
    console.log(value)
  })
};
